package com.example.shiwuyouproject.ui.fragment.my.activity

import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityAcademiccertificationBinding
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.AcademicCertificationViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.GlideUtils
import com.example.shiwuyouproject.utils.MyTimeUtils
import com.luck.picture.lib.config.PictureConfig
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import java.util.*

/**
 * 学历认证
 */
class AcademicCertificationActivity :
    BaseVmActivity<ActivityAcademiccertificationBinding, AcademicCertificationViewModel>() {
    override fun viewModelClass(): Class<AcademicCertificationViewModel> =
        AcademicCertificationViewModel::class.java

    override fun getViewBinding(): ActivityAcademiccertificationBinding {
        return ActivityAcademiccertificationBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mViewModel.getRealNameAuthenticationData()
    }

    override fun initData() {
        initCustomTimePicker()

    }

    private var eduimg = ""
    private var eduend_time = ""
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == PictureConfig.REQUEST_CAMERA) {
            QiNiuUpImageUtils.postUpQiNiuFile(data, object : QiNiuUpImageUtils.onBackImageStatus {
                override fun onSuccess(key: String) {
                    eduimg = key
                    GlideUtils.fangImgPortrait(
                        this@AcademicCertificationActivity,
                        mBinding.ivEduimg,
                        eduimg
                    )
                }

                override fun onFail() {
                    YUtils.hideLoading()
                    ToastUtil.show("图片上传失败，请检查网络连接")
                }
            })
        }
    }

    private var eduno = ""
    override fun setListener() {
        mBinding.eduendTime.setOnClickListener { //选择时间
            pvCustomLunar?.show()
        }
        mBinding.btSava.setOnClickListener { //保存
            eduno = mBinding.eduno.text.toString()
            if (eduend_time.isEmpty()) {
                ToastUtil.show("请选择毕业时间！")
                return@setOnClickListener
            }
            if (eduno.isEmpty()) {
                ToastUtil.show("请输入证书编号！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "保存中...")
            mViewModel.jiaoxuetedian(eduend_time, eduimg, eduno)
        }
        mBinding.clUploadTheFrontOfIDCard.setOnClickListener {
            QiNiuUpImageUtils.openCard(this)
        }
    }

    override fun observe() {
        mViewModel.getGenRenStatus.observe(this,
            Observer<PersonalInformationBean> {
                mBinding.eduendTime.text = it.eduend_time
                eduend_time = it.eduend_time
                mBinding.eduno.setText(it.eduno)
                eduno = it.eduno
                GlideUtils.fangImgPortrait(this, mBinding.ivEduimg, it.eduimg)
                eduimg = it.eduimg
            })
        mViewModel.getPostStatus.observe(this, Observer<Boolean> {
            if (it) {
                YUtils.hideLoading()
                ToastUtil.show("提交成功！")
                finish()
            }

        })
    }

    var pvCustomLunar: TimePickerView? = null
    var cuurDataTime: Date? = null
    private fun initCustomTimePicker() {
        val selectedDate = Calendar.getInstance() //系统当前时间
        val startDate = Calendar.getInstance()
        startDate[1990, 1] = 23
        val endDate = Calendar.getInstance()
        endDate[2069, 2] = 28
        //时间选择器 ，自定义布局
        //时间选择器 ，自定义布局
        pvCustomLunar = TimePickerBuilder(
            this
        ) { date, v -> //选中事件回调
            cuurDataTime = date
        }
            .setDate(selectedDate)
            .setRangDate(startDate, endDate)
            .setLayoutRes(
                R.layout.pickerview_custom_lunar
            ) { v ->
                val tvSubmit = v.findViewById<View>(R.id.tv_finish) as TextView
                val ivCancel = v.findViewById<View>(R.id.iv_cancel) as TextView
                tvSubmit.setOnClickListener {
                    pvCustomLunar?.returnData()
                    pvCustomLunar?.dismiss()
                    eduend_time =
                        cuurDataTime?.let { it1 -> MyTimeUtils.getTimes(it1).toString() }
                            .toString()
                    mBinding.eduendTime.text = eduend_time
                }
                ivCancel.setOnClickListener { pvCustomLunar?.dismiss() }
            }
            .setDividerColor(Color.WHITE)//设置分割线的颜色
            .setType(booleanArrayOf(true, true, false, false, false, false))
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .setDividerColor(Color.RED)
            .build()
    }
}